/*
* Licensed under the Apache License, Version 2.0 (the "License"):
*    http://www.apache.org/licenses/LICENSE-2.0
*/
package ui.maps.googlemap.custom {
	
	import com.google.maps.LatLng;
	import com.google.maps.MapEvent;
	import com.google.maps.PaneId;
	import com.google.maps.interfaces.IMap;
	import com.google.maps.interfaces.IPane;
	import com.google.maps.overlays.OverlayBase;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import spark.components.Label;
	
	public class TooltipOverlay extends OverlayBase {
		private var latLng:LatLng;
		private var label:String;
		private var textField:TextField;
		private var isCenter:Boolean;
		private var iconHeight:Number = 0;
		
		public function TooltipOverlay(latLng:LatLng, label:String, isCenter:Boolean) {
			super();
			this.latLng = latLng;
			this.label = label;
			this.isCenter = isCenter;
			this.useHandCursor = true;
			
			this.addEventListener(MapEvent.OVERLAY_ADDED, onOverlayAdded);
			this.addEventListener(MapEvent.OVERLAY_REMOVED, onOverlayRemoved);
		}
		
		public override function getDefaultPane(map:IMap):IPane {
			return map.getPaneManager().getPaneById(PaneId.PANE_FLOAT);
		}
		
		public function addEvent(event:String,func:Function):void{
			if(this.textField){
				this.textField.addEventListener(event,func);
			}
		}
		
		private function onOverlayAdded(event:MapEvent):void {
			this.textField = new TextField();
			
			if(isCenter == true){
				var newFormat:TextFormat = new TextFormat();
				newFormat.size = 14;
				newFormat.bold = true;
				textField.defaultTextFormat = newFormat;
			}
			
			this.textField.text = this.label==null?"":this.label;
			this.textField.selectable = false;
			this.textField.background = !isCenter;
			this.textField.multiline = false;
			this.textField.autoSize = TextFieldAutoSize.CENTER;
			this.textField.backgroundColor = 0x004E98;
			this.textField.textColor = 0xffffff;
			this.addChild(textField);
		}
		
		private function onOverlayRemoved(event:MapEvent):void {
			this.removeChild(textField);
			this.textField = null;
		}
		
		public override function positionOverlay(zoomChanged:Boolean):void {
			var point:Point = this.pane.fromLatLngToPaneCoords(this.latLng);
			this.textField.x = point.x - this.textField.width / 2;
			this.textField.y = point.y;
//			if(isCenter){
//				this.textField.y = point.y - this.textField.height/2;
//			}else if(iconHeight > 0){
//				this.textField.y = point.y + iconHeight/2;
//			}else{
//				this.textField.y = point.y + this.textField.height/2 + 5;
//			}
		}
		
		public function resetY(iconHeight:Number):void{
			if(isCenter){
				return;
			}
			this.iconHeight = iconHeight;
			var point:Point = this.pane.fromLatLngToPaneCoords(this.latLng);
			this.textField.y = point.y + iconHeight/2;
		}
	}
}